<template>
  <el-container style="height: 100vh">
    <el-aside :width="asideWidth" class="aside">
      <div class="avatar_box">
        <img :src="logoSrc" alt="logo" class="avatar" />
        <span v-show="!isCollapsed" class="system_name">海报设计系统</span>
      </div>
    </el-aside>
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <i
            class="el-icon-s-fold"
            v-if="!isCollapsed"
            @click="toggleAside"
          ></i>
          <i
            class="el-icon-s-unfold"
            v-if="isCollapsed"
            @click="toggleAside"
          ></i>
          <!-- Breadcrumb Component -->
          <el-breadcrumb
            separator-class="el-icon-arrow-right"
            class="breadcrumb"
          >
            <el-breadcrumb-item
              v-for="(item, index) in breadcrumbItems"
              :key="index"
              :to="item.to"
              >{{ item.label }}</el-breadcrumb-item
            >
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-avatar :src="avatarSrc" size="medium"></el-avatar>
          <el-dropdown>
            <span class="el-dropdown-link">
              admin<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>个人中心</el-dropdown-item>
              <el-dropdown-item>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <el-main class="main">Main</el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: "1", // 设置默认激活的菜单项
      isCollapsed: false, // 控制侧边栏展开和收缩的状态变量
      logoSrc: require("../assets/logo.jpg"), // 引用 logo 图像
      avatarSrc: require("../assets/tx.jpg"), // 引用头像图像
      breadcrumbItems: [{ label: "首页", to: "/" }], // 初始化面包屑路径
    };
  },
  computed: {
    asideWidth() {
      return this.isCollapsed ? "64px" : "200px";
    },
  },
  methods: {
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
      // 根据选择的菜单项更新面包屑路径
      this.updateBreadcrumb(key);
    },
    toggleAside() {
      this.isCollapsed = !this.isCollapsed;
    },
    updateBreadcrumb(key) {
      const breadcrumbMap = {
        1: [{ label: "首页", to: "/" }],
        "2-1": [
          { label: "首页", to: "/" },
          { label: "系统管理", to: "/system" },
          { label: "用户管理", to: "/system/user" },
        ],
        "2-2": [
          { label: "首页", to: "/" },
          { label: "系统管理", to: "/system" },
          { label: "菜单管理", to: "/system/menu" },
        ],
        "2-3": [
          { label: "首页", to: "/" },
          { label: "系统管理", to: "/system" },
          { label: "角色管理", to: "/system/role" },
        ],
        3: [
          { label: "首页", to: "/" },
          { label: "海报管理", to: "/poster" },
        ],
        4: [
          { label: "首页", to: "/" },
          { label: "素材管理", to: "/materials" },
        ],
      };
      this.breadcrumbItems = breadcrumbMap[key] || [{ label: "首页", to: "/" }];
    },
  },
};
</script>

<style scoped>
html,
body,
#app {
  height: 100%;
  margin: 0;
}

.el-container {
  height: 100%;
}

.el-aside {
  background-color: #333;
  color: #fff;
  transition: width 0.3s ease; /* 添加宽度过渡效果 */
}

.avatar_box {
  display: flex;
  align-items: center;
  padding-left: 12px;
  background-color: #444;
  height: 60px; /* 与header一致 */
  border-bottom: 1px solid #555;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}

.system_name {
  font-size: 16px;
  font-weight: bold;
  color: #fff;
}

.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.header-right .el-avatar {
  margin-right: 10px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  flex-grow: 1; /* 确保main部分占据剩余的高度 */
}

.breadcrumb {
  margin-left: 20px;
  color: #333;
  font-size: 14px;
}

::v-deep .el-icon-arrow-right:before {
  color: black;
}

.breadcrumb .el-breadcrumb__inner {
  font-size: 14px;
}

/* 自定义菜单样式 */
.el-menu-vertical-demo {
  background-color: #333;
  border-right: none;
}

::v-deep .el-menu-item,
::v-deep .el-submenu__title {
  background-color: #333;
  color: #fff;
}

::v-deep .el-menu-item:hover,
::v-deep .el-submenu__title:hover,
::v-deep .el-menu-item.is-active {
  background-color: #444 !important;
}

/* 隐藏收缩状态下的子菜单箭头图标 */
::v-deep .el-submenu__icon-arrow {
  display: none;
}
</style>
